package com.ruoyi.project.gxgw_modules.input.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.goods.service.IGoodsService;
import com.ruoyi.project.gxgw_modules.input.domain.Input;
import com.ruoyi.project.gxgw_modules.input.service.IInputService;
import com.ruoyi.project.gxgw_modules.input.vo.InputVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 商品入库 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-21
 */
@Controller
@RequestMapping("/gxgw_modules/input")
public class InputController extends BaseController {
	private String prefix = "gxgw_modules/input";

	@Autowired
	private IInputService inputService;

	@Autowired
	private IGoodsService goodsService;

	@RequiresPermissions("gxgw_modules:input:view")
	@GetMapping()
	public String input() {
		return prefix + "/input";
	}

	/**
	 * 查询商品入库列表
	 */
	@RequiresPermissions("gxgw_modules:input:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Input input) {
		startPage();
		List<InputVo> list = inputService.selectInputList(input);
		return getDataTable(list);
	}

	/**
	 * 导出商品入库列表
	 */
	@RequiresPermissions("gxgw_modules:input:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(InputVo input) {
		List<InputVo> list = inputService.selectInputList(input);
		ExcelUtil<InputVo> util = new ExcelUtil<InputVo>(InputVo.class);
		return util.exportExcel(list, "input");
	}

	/*	*//**
			 * 新增商品入库
			 *//*
				 * @GetMapping("/add") public String add() { return prefix + "/add"; }
				 */

	/**
	 * 新增保存商品入库
	 *//*
		 * @RequiresPermissions("gxgw_modules:input:add")
		 * 
		 * @Log(title = "商品入库", businessType = BusinessType.INSERT)
		 * 
		 * @PostMapping("/add")
		 * 
		 * @ResponseBody public AjaxResult addSave(Input input) { Date date = new
		 * Date(); Timestamp timeStamp = new Timestamp(date.getTime());
		 * input.setInputTime(timeStamp); input.setAdminId(1);
		 * 
		 * return toAjax(inputService.insertInput(input)); }
		 */

	/**
	 * 修改商品入库
	 */
	@GetMapping("/edit/{inputId}")
	public String edit(@PathVariable("inputId") Integer inputId, ModelMap mmap) {
		Input input = inputService.selectInputById(inputId);
		 
	    Timestamp timeStamp = new Timestamp(input.getInputTime().getTime()); 
	    
	    input.setInputTime(timeStamp);
		mmap.put("input", input);
		return prefix + "/edit";
	}

	/**
	 * 修改保存商品入库
	 */
	@RequiresPermissions("gxgw_modules:input:edit")
	@Log(title = "商品入库", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Input input) {
		//从数据中查询审核状态
		int judge = inputService.selectCheckState(input.getInputId());
        //如果前端选择的状态为审核通过
		if(input.getCheckState() == 1) {
			
		//检查之前是否通过审核，如果是，则不更新数目
		     if (judge == 1) {
				return error(1,"审核已通过！禁止重复审核");
		}
		goodsService.addNum(input.getGoodsId(), input.getInputNum());
		}
		//如果前端选择的状态不为1
		else {
			//检查之前是否通过审核，如果是，则不允许修改审核的状态
			if (judge == 1) {
				return error(1,"审核已通过！禁止重复审核");
				}
		}
		
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		input.setCheckTime(timeStamp);
		// 设置修改人
		input.setCheckName(ShiroUtils.getSysUser().getUserName());
		return toAjax(inputService.updateInput(input));
	}

	/**
	 * 删除商品入库
	 */
	@RequiresPermissions("gxgw_modules:input:remove")
	@Log(title = "商品入库", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(inputService.deleteInputByIds(ids));
	}

}
